package net.tycmc.cty.ctyms.timejob.iems.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.tycmc.functions.util.StringUtil;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.dao.ITransactionDAO;
//import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.dbutil.global.GlobalDBVar;
import net.tycmc.cty.ctyms.timejob.common.EquipVersionCommon;
//import net.tycmc.cty.ctyms.timejob.common.ConfigEnum;
import net.tycmc.cty.ctyms.timejob.common.NumberFormat;


public class SensorAlarmDAO {
	private IBaseDAO baseDAO;
	private ITransactionDAO tranDAO;
	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}	
	public void setTranDAO(ITransactionDAO tranDAO) {
		this.tranDAO = tranDAO;
	}

	/**
	 * 获取此次处理的传感器报警ID,
	 **/
	public Map<String,Object> getEventIDDef() {
		String sql = "select top 1 StatR_StatDataID ,StatR_MsgTime from StatRecord where StatR_Type = 6 and StatR_OprtType = 1 ";
		return this.baseDAO.execQueryOne(sql, new Object[]{});
	}
	/**
	 * 获取此次处理的传感器报警信息
	 **/
	public List<Map<String, Object>> queryNeedWorkSensorAlarm(
			Map<String,Object> flagSensor) {
		List<Map<String,Object>> reList = null;
		//如果此次需要处理的报警信息ID数据 为null，则直接返回
		if(null==flagSensor||flagSensor.isEmpty()){
			
		}else{
			StringBuffer strBuf = new StringBuffer();
			strBuf.append("select vv.VehicleNumber,vv.VehicleID,vv.DBCode,ma.MsgAlm_ID,ma.MsgAlm_MsgTime,ma.MsgAlm_La,ma.MsgAlm_Lo,mac.MsgAlmC_SysA_ID, ");
			strBuf.append("MsgAlmC_SysA_Name=(select SysA_ItemCN from CTY_Egn_B.dbo.Sys_Alarm where SysA_ID = mac.MsgAlmC_SysA_ID), ");
			strBuf.append("mac.MsgAlmC_IsAlarm,mac.MsgAlmC_Value,mac.MsgAlmC_ID  ");
			strBuf.append("from V_VehicleInfo vv inner join CTY_Egn_B.dbo.Msg_Alarm ma on vv.VehicleID = ma.MsgAlm_Vcl_ID  ");
			strBuf.append("inner join CTY_Egn_B.dbo.Msg_Alarm_Content mac on mac.MsgAlmC_MsgAlm_ID = ma.MsgAlm_ID  ");
			strBuf.append("where ma.MsgAlm_ID >'"+MapGetter.getString(flagSensor, "StatR_StatDataID")+"' and ma.MsgAlm_MsgTime >= '"+MapGetter.getString(flagSensor, "StatR_MsgTime")+"' and mac.MsgAlmC_SysA_ID in (17,18)");
			//3.6.8 fujichuan 2014-09-27 新增加版本不支持
			strBuf.append("and vv.UsefulLife > GETDATE() and vv.Tmnl_SoftEdition is not null and vv.Tmnl_SoftEdition <> '' and vv.Tmnl_SoftEdition in ("+EquipVersionCommon.getSoftVerByAgrType("SensorAlarm_JPP")+") order by MsgAlm_MsgTime ");
			reList = this.baseDAO.execQuery(strBuf.toString(), new Object[]{});
		}
		return reList;
	}
	/**
	 * 获取提示表中最新的已解除的传感器报警序列
	 **/
	public List<Map<String, Object>> queryPromptOfSensorAlarm() {
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("select MsgP_VclID,Msgp_SecTypeID from Msg_Prompt where MsgP_State = 1 and MsgP_Type = 14 ");
		strBuf.append("and exists(select 1 from V_VehicleInfo where VehicleID = MsgP_VclID and UsefulLife > GETDATE() and Tmnl_SoftEdition is not null and Tmnl_SoftEdition <> '')");
		return this.baseDAO.execQuery(strBuf.toString(), new Object[]{});
	}
	/**
	 * 更新传感器报警标志
	 **/
	public boolean updateFlagOfSensor(Map<String, Object> map) {
		String sql  = "update StatRecord set StatR_StatDataID = ?, StatR_MsgTime = ? ,StatR_UpdateTime = GETDATE() from StatRecord where StatR_Type = 6 and StatR_OprtType = 1 ";
		int i = this.baseDAO.execUpdate(sql, new Object[]{MapGetter.getString(map,"MsgAlm_ID"),MapGetter.getString(map,"MsgAlm_MsgTime")});
		if(i>-1){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 获取录入提示表的SQL
	 **/
	public void getInsertPromptSQL(List<String> insertPrompt,
			List<Object[]> paramPrompt, Map<String, Object> oneAlarm, int type,String userSMS) {
		insertPrompt.add("insert into Msg_Prompt(MsgP_VclID,MsgP_MsgID,MsgP_MsgCID,MsgP_MsgTime,MsgP_Type,MsgP_State,MsgP_DBName," +
				"MsgP_TableName,MsgP_UpdateTime,MsgP_CreateTime,MsgP_DetailMsg,MsgP_SMSRecUser,MsgP_SecTypeId,MsgP_SecTypeName," +
				"MsgP_SensorValue)values(?,?,?,?,?,?,?,?,GETDATE(),GETDATE(),?,?,?,?,?)");
		String[] detail = this.getSensorDetail(type,oneAlarm);
		paramPrompt.add(new Object[]{
				MapGetter.getString(oneAlarm, "VehicleID"),
				MapGetter.getString(oneAlarm, "MsgAlm_ID"),
				MapGetter.getString(oneAlarm, "MsgAlmC_ID"),
				MapGetter.getString(oneAlarm, "MsgAlm_MsgTime"),
				"14",
				"1",
				"B",
				"Msg_Alarm",
				detail[1],
				userSMS,
				MapGetter.getString(oneAlarm, "MsgAlmC_SysA_ID"),
				MapGetter.getString(oneAlarm, "MsgAlmC_SysA_Name"),
				detail[0]
				});
	}
	/**
	 * 提示表详情字段值拼接
	 **/
	private String[] getSensorDetail(int type,Map<String, Object> oneAlarm) {
		String[] re = new String[2];
		switch(type){
		case(17):
			re[0] = StringUtil.isValid(MapGetter.getString(oneAlarm,"MsgAlmC_Value"))?String.valueOf(NumberFormat.round((Double.parseDouble(MapGetter.getString(oneAlarm,"MsgAlmC_Value"))),1)):"";
			re[1] = "温度报警，"+re[0]+"℃";
			break;
		case(18):
			re[0] = "烟度异常";
		    re[1] = "烟度报警，"+re[0];
			break;
		default:
			break;
		}
		return re;
	}
	/**
	 * 更新传感器报警状态
	 **/
	public void getUpdatePromptSQL(List<String> insertPrompt,
			List<Object[]> paramPrompt, Map<String, Object> oneAlarm, int type,
			String userSMS) {
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("update Msg_Prompt set MsgP_State = 3,MsgP_ReleaseTime = ?, MsgP_ReleaseType = 1,MsgP_RelsCID = ? ");
		if(StringUtil.isValid(userSMS)){
			strBuf.append(" ,MsgP_SMSRecUser = MsgP_SMSRecUser+"+userSMS.substring(1)+" ");
		}
		strBuf.append("where MsgP_VclID = ? and MsgP_SecTypeId = ? and MsgP_Type = 14 and MsgP_State = 1");
		insertPrompt.add("update Msg_Prompt set MsgP_State = 3,MsgP_ReleaseTime = ?, MsgP_ReleaseType = 1,MsgP_RelsCID = ? where MsgP_VclID = ? and MsgP_SecTypeId = ? and MsgP_Type = 14 and MsgP_State = 1");
		paramPrompt.add(new Object[]{
				MapGetter.getString(oneAlarm, "MsgAlm_MsgTime"),
				MapGetter.getString(oneAlarm, "MsgAlmC_ID"),
				MapGetter.getString(oneAlarm, "VehicleID"),
				MapGetter.getString(oneAlarm, "MsgAlmC_SysA_ID")
				});
		
	}
	/**
	 * 批量提交SQL
	 **/
	public boolean batchSumbitSQL(List<String> sqlList,
			List<Object[]> paramList) {
		boolean flag = true;
		if(null!=sqlList && !sqlList.isEmpty() && sqlList.size() == paramList.size()){
			flag = this.tranDAO.batch(sqlList, paramList);
		}
		return flag;
	}
	/**
	 * 校验某个设备在非工作时间是否已发送过短信
	 *
	public boolean nonWorkIsOccured(String SMSCfgType,
			String tipTimeColumnName, String nowTipTime, String isOccuredSQL,String MsgAlmC_IsAlarm) {
		String sql = "select * from SMS_Config where SC_Type=?";
		Map<String, Object> hm = this.baseDAO.execQueryOne(sql,
				new Object[] { SMSCfgType });
		String nowTimeStr = DateUtil.toString(DateUtil.now());
		String SC_SendTime_Bgn = hm.get("SC_SendTime_Bgn") != null ? nowTimeStr
				.substring(0, 10)
				+ " "
				+ hm.get("SC_SendTime_Bgn").toString()
				+ ":00" : ConfigEnum.SendTime_Bgn.getName();
		String SC_SendTime_End = hm.get("SC_SendTime_End") != null ? nowTimeStr
				.substring(0, 10)
				+ " "
				+ hm.get("SC_SendTime_End").toString()
				+ ":00" : ConfigEnum.SendTime_End.getName();

		String SC_SendTime_Bgn_Next = hm.get("SC_SendTime_Bgn") != null ? DateUtil
				.addDay(1) + " " + hm.get("SC_SendTime_Bgn").toString() + ":00"
				: ConfigEnum.SendTime_Bgn.getName();
		String SC_SendTime_End_Up = hm.get("SC_SendTime_End") != null ? DateUtil
				.addDay(-1)
				+ " "
				+ hm.get("SC_SendTime_End").toString()
				+ ":00" : ConfigEnum.SendTime_End.getName();

		// 条件
		String where = " and (" + tipTimeColumnName
				+ " between convert(datetime,'" + SC_SendTime_End + "',120) "
				+ "and convert(datetime,'" + SC_SendTime_Bgn_Next
				+ "',120) and convert(datetime,'" + nowTipTime
				+ "',120)>convert(datetime,'" + SC_SendTime_End + "',120)) or "
				+ "(" + tipTimeColumnName + " between convert(datetime,'"
				+ SC_SendTime_End_Up + "',120) and convert(datetime,'"
				+ SC_SendTime_Bgn + "',120) " + "and convert(datetime,'"
				+ nowTipTime + "',120)<convert(datetime,'" + SC_SendTime_Bgn
				+ "',120))";
		String retSQL = isOccuredSQL + where;
		List<Map<String, Object>> smsList = baseDAO.execQuery(retSQL,
				new Object[] {});
		boolean flag = true;
		if(null==smsList||smsList.isEmpty()){
			flag = false;
		}else{
			String IsAlarmstat = "";
			for(Map<String,Object> one:smsList){
				IsAlarmstat += MapGetter.getString(one, "cnt");
			}
			if(IsAlarmstat.indexOf(MsgAlmC_IsAlarm)>-1){
				flag = true;
			}else{
				flag = false;
			}
		}
		return flag;
	}
	*/
	/*从提示开关表中获取传感器提示开关为关的设备号集合20141016wangkai
	 * 3.6.6新增
	 * vclpromptswitch:提示开关表
	 * VclPS_isOpen=0：开关状态为关
	 * VclPS_Type=4：提示开关类型为传感器
	 * */
	public List<String> getSensorPromtPSwitch() {
		List<String> SensorPromSwitch = new ArrayList<String>();

		String sql = "select VclPS_VclID from " +GlobalDBVar.getDBFullName_Conn("VclPromptSwitch","1",null)+
				" where VclPS_isOpen=0 and VclPS_Type=4 ";
		List<Map<String, Object>> list = baseDAO.execQuery(sql, new Object[] {});
		for (Map<String, Object> map : list) {
			SensorPromSwitch.add(MapGetter.getString(map, "VclPS_VclID"));
		}
		return SensorPromSwitch;
	}
}